//工具集合Tools
window.T = {};
var token = sessionStorage.getItem("token");
layui.config({
    base: "js/"
}).use(['jquery'], function () {
   var index=-1;
    //jquery全局配置
    layui.$.ajaxSetup({
        dataType: "json",
        cache: false,
        beforeSend: function(){
            index = layer.load(3, {
                shade: [0.5,'#030303'] //0.1透明度的白色背景
            });
        },
        complete: function (xhr) {
            layer.close(index);
            if(xhr.status == 'undefined'){
                location.href="/page/404.html";
            }
            switch(xhr.status){
                case 403:
                    // 未授权异常
                    alert("系统拒绝：您没有访问权限。");
                    break;
                case 404:
                    location.href="/page/404.html";
                    break;
                case 401:
                    top.location.href = '/login.html';
            }

        },
        // 出错时默认的处理函数
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var msg=XMLHttpRequest.responseJSON.error_description;
            if(isNotNull(msg)){
                alert(msg);
            }
        }
    });
});
function isNotNull(variable1) {
    if (variable1 === null || variable1 === 'undefined' || variable1 === '') {
        return  false;
    }else {
        return true;
    }
}
// 获取请求参数
// 使用示例
// location.href = http://localhost/index.html?id=123
// T.p('id') --> 123;
var url = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
};
T.p = url;

var host = window.location.host;
var i = host.indexOf(":");
host = i > 0 ? host.substr(0, i) : host;
//当前父级URL
var baseURL = "http://" + host + ":8568/";
console.log(baseURL);
//请求前缀 认证中心
var baseOauthURL = baseURL+"uat/";
//规则中心url
var baseRuleURL= baseURL;

var baseKnowledgeUrl=baseURL+"knowledge";

//jqgrid全局配置
/* $.extend($.jgrid.defaults, {
     ajaxGridOptions: {
         headers: {
             "token": token
         }
     }
 });*/

//权限判断
function hasPermission(permission) {
    if (window.parent.permissions.indexOf(permission) > -1) {
        return true;
    } else {
        return false;
    }
}

//重写alert
window.alert = function (msg, callback) {
    parent.layer.alert(msg, function (index) {
        parent.layer.close(index);
        if (typeof(callback) === "function") {
            callback("ok");
        }
    });
};

//重写confirm式样框
window.confirm = function (msg, callback) {
    parent.layer.confirm(msg, {btn: ['确定', '取消']},
        function () {//确定事件
            if (typeof(callback) === "function") {
                callback("ok");
            }
        });
};

function sysDict(type, value) {
    var json = localStorage.getItem("sysDict");
    if(json){
        var sysDict = JSON.parse(json);
        for ( x in sysDict){
            if (sysDict[x].type == type && sysDict[x].value == value) {
                return sysDict[x];
            }
        }
    }
    return null;
}